/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.classdiagram.parsers.texteditor.portlabel;

import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.contentassist.ContentAssistant;
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
import org.eclipse.jface.text.contentassist.IContentAssistant;
import org.eclipse.jface.text.presentation.IPresentationReconciler;
import org.eclipse.jface.text.presentation.PresentationReconciler;
import org.eclipse.jface.text.rules.BufferedRuleBasedScanner;
import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewerConfiguration;
import org.eclipse.swt.graphics.Color;
import org.eclipse.uml2.uml.Port;

import com.cea.papyrus.classdiagram.parsers.texteditor.ILabelColorConstants;
import com.cea.papyrus.classdiagram.parsers.texteditor.LabelColorProvider;

/**
 * This class bundles the configuration space of the action language source
 * viewer. Instances of this class are passed to the configure method of
 * <code>SourceViewer</code>.
 * 
 * @author Remi SCHNEKENBURGER
 * @see org.eclipse.jface.text.source.SourceViewerConfiguration
 */
public class PortLabelSourceViewerConfiguration extends SourceViewerConfiguration {

	/** port to edit */
	private Port port;

	/**
	 * Creates a new PortLabelSourceViewerConfiguration
	 */
	public PortLabelSourceViewerConfiguration() {
	}

	/**
	 * 
	 */
	static class SingleTokenScanner extends BufferedRuleBasedScanner {
		/**
		 * @param attribute 
		 */
		public SingleTokenScanner(TextAttribute attribute) {
			setDefaultReturnToken(new Token(attribute));
		}
	};

	/**
	 * {@inheritDoc}
	 */
	@Override
	public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
		PresentationReconciler reconciler = new PresentationReconciler();

		// creating damager/repairer for single line comments
		Color colorString = LabelColorProvider.getDefault().getColor(ILabelColorConstants.STRING);
		DefaultDamagerRepairer dr = new DefaultDamagerRepairer(
				new SingleTokenScanner(
						new TextAttribute(colorString)));

		// creating damager/repairer for other zones (action language zone)
		dr = new DefaultDamagerRepairer(new PortLabelScanner());
		reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
		reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
		return reconciler;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
		ContentAssistant assistant = new ContentAssistant();

		if(getPort()!=null) {
			IContentAssistProcessor processor = new PortLabelCompletionProcessor(getPort());
			assistant.setContentAssistProcessor(processor,IDocument.DEFAULT_CONTENT_TYPE);

			// IInformationControlCreator
			assistant.setInformationControlCreator(
					getInformationControlCreator(sourceViewer));

			assistant.enableAutoActivation(true);
			assistant.setAutoActivationDelay(500);
		}
		return assistant;
	}

	/**
	 * @return the port
	 */
	public Port getPort() {
		return port;
	}

	/**
	 * @param port the port to set
	 */
	public void setPort(Port port) {
		this.port = port;
	}
}